home *** CD-ROM | disk | FTP | other *** search
- On Tue, 31 Jan 1995, Flint wrote:
-
- [...]
- >
- > How does recursive programming work?
- >
-
- Basically many problems can be solved by having some subtask call
- itself as part of the sol'n. This is recursion, ie. programs that call
- themselves are recursive programs. Why use recursion ? Some recursive
- solutions can be very short and simple compared to other iterative
- solutions. As an example consider the problem of a 'Flooding' Algorithm.
- You know the 'paint' command in Amos (Note: Amos uses the OS to do its
- flooding). You can code it many different ways, and most use some
- explicit use of some stack, but the simplest and most elegant (subjective)
- is just to use recursion. The disadvantage is that this simple method
- is less effecient. Here is an simple recursive algorithm for flooding
- ar area: (pseudo Amos :) )
-
-
- procedure Flood( x , y , color )
-
- if( Point(x,y) <> color )
-
- Ink color : Plot x,y
-
- Flood( x+1 , y , color )
- Flood( x-1 , y , color )
- Flood( x , y+1 , color )
- Flood( x , y-1 , color )
-
- end if
-
- end proc
-
-
- This works, by calling itslef on the 4 neighbouring pixels around
- the pixel (x,y). You just have to call it the first time with the
- start pixel (x,y).
-
- > Suppose I wanted to make a
- > MineSweeper clone, how would I let the computer show all the fields
- > that aren't surrounded by a bomb whenever I hit a field which is not
- > surrounded by a bomb?
- > Mind, I'm not trying to make a clone here, I just thought it's the best
- > way to illustrate whatever I try to... well, er, try, really :)
- >
-
- This is very much like the 'Flood' algorirthm above ( coincedence
- :) )...
-
-
- Procedure Show_Mines( mine_x , mine_y )
-
- if( Number_of_Mines( mine_x, mine_y ) = 0 )
- ' surrounded by no mines
-
- ' do whatever code you have to do to
- ' actually show that this mine co-ordinate (x,y)
- ' has actually been turned up/tested/etc...
-
- For y = -1 to 1
- For x = -1 to 1
- if( x<>0 and y<>0)
- Show_Mines( mine_x + x , mine_y + y )
- endif
- Next
- Next
-
- end if
- end procedure
-
-
-
- I hope this helps...
-
- Michael Sikorsky..
-
- > Flint.
- >
- >
- > "My boy, if ever you are lost at sea, drop right in and think of me."
- > - J. Heller
- >
- >
-
-